/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.biblioteca.dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.its.dsw2.biblioteca.enumeracion.EstadoDetallePrestamo;
import mx.its.dsw2.biblioteca.modelo.DetallePrestamo;
import mx.its.dsw2.biblioteca.modelo.Libro;
import mx.its.dsw2.biblioteca.modelo.Prestamo;

/**
 *
 * @author Maq-8
 */
@Stateless
public class DetallePrestamoDao extends AbstractDao<DetallePrestamo> implements DetallePrestamoDaoLocal {

    @PersistenceContext(unitName = "Biblioteca8E-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DetallePrestamoDao() {
        super(DetallePrestamo.class);
    }

    @Override
    public List<DetallePrestamo> findLends(Prestamo lend) {
        Query q = em.createNamedQuery("DetallePrestamo.findLends");
        q.setParameter("estado", EstadoDetallePrestamo.PRESTADO.ordinal());
        q.setParameter("idPrestamo", lend.getId());
        return q.getResultList();
    }

    @Override
    public DetallePrestamo findBooks(Libro book) {
        Query q = em.createNamedQuery("DetallePrestamo.findBooks");
        q.setParameter("estado", EstadoDetallePrestamo.PRESTADO.ordinal());
        q.setParameter("idLibro", book.getId());
        if (q.getResultList().isEmpty()) {
            return null;
        } else {
            return (DetallePrestamo) q.getResultList().get(0);
        }
    }
}
